
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;


public partial class StoredProcedures
{
	[SqlProcedure]
	public static void SpMedia(out SqlSingle med)
	{
		// Put your code here
		int cont = 0;
		float f = 0;
        SqlConnection cn = new SqlConnection("context connection=true");
        cn.Open();
        SqlCommand cmd = cn.CreateCommand();
        cmd.CommandText = "Select valor from tabela1";
		SqlDataReader rdr = cmd.ExecuteReader();

		while (rdr.Read())
		{
			f += rdr.GetFloat(0);
			++cont;
		}
		rdr.Close();
		med = (cont == 0) ? -1 : f / cont;
	}
}
